#include "cc_wifi.h"
#include "cc_mqtt.h"
#include "cc_shell.h"
#include "cc_fs.h"

#if (CC_CONFIG_USE_WIFI)
char wifi[20] = "default";
char pwd[20] = "default";

int cc_wifi_init()
{
  if (cc_get_wifi_state() == CC_WIFI_CONNECTED)
  {
    return -1;
  }
  static int cnt;

  if (!cc_fs_get_bool("f_w_wifi"))
  {
    cc_fs_set_string("wifi_id", wifi);
    cc_fs_set_string("wifi_pwd", pwd);
    cc_fs_set_bool("f_w_wifi", cc_true);
    cc_log("first write wifi:%s  %s\r\n", wifi, pwd);
  }
  else
  {
    memset(wifi, 0, sizeof(wifi));
    memset(pwd, 0, sizeof(pwd));
  }
  cc_log("get fs wifi name:%s\r\n", cc_fs_get_string("wifi_id").c_str());
  cc_log("get fs wifi pwd:%s\r\n", cc_fs_get_string("wifi_pwd").c_str());

  memcpy(wifi, cc_fs_get_string("wifi_id").c_str(), 20);
  memcpy(pwd, cc_fs_get_string("wifi_pwd").c_str(), 20);

  cc_log("connect wifi:%s pwd:%s\r\n", wifi, pwd);
  WiFi.mode(WIFI_STA);
  WiFi.begin(wifi, pwd);
  // 官方这个函数有bug 第二次重连其实已经连上了 但是返回的是没有wifi名称
  while (WiFi.status() != WL_CONNECTED)
  {
    cc_sleep(1000);
    cc_printf(".");

    cnt++;
    if (cnt == 5)
    {
      if (WiFi.status() == WL_CONNECTED)
      {
        cc_log("WiFi connected::%s\r\n", WiFi.localIP().toString().c_str());
        return 1;
      }
      cc_log_e("\r\nwifi connect failed err:%d\r\n", WiFi.status());
      cnt = 0;
      return -1;
    }
  }
  cc_log("WiFi connected::%s\r\n", WiFi.localIP().toString().c_str());
  return 1;
}
CC_INIT_PREV_REGISTER(cc_wifi_init);

int cc_get_wifi_state()
{
  return WiFi.status();
}

int cc_wifi_disconnect()
{
  return WiFi.disconnect(true);
}

void show_wifi(char* param, int node)
{
  int flag = 0;
  char temp_wifi[CC_CONFIG_WIFI_MAX_SSID_LEN];
  char temp_pwd[CC_CONFIG_WIFI_MAX_PWD_LEN];
  memset(temp_wifi, 0, sizeof(temp_wifi));
  memset(temp_pwd, 0, sizeof(temp_pwd));
  cc_log("recv from node(%d) data(%s):\r\n", node, param);
  int arg_cnt = cc_shell_parse_args(param, "%s %s", temp_wifi, temp_pwd);
  if (strcmp(temp_wifi, "") != 0 && strcmp(temp_wifi, wifi) != 0)
  {
    memcpy(wifi, temp_wifi, CC_CONFIG_WIFI_MAX_SSID_LEN);
    cc_fs_set_string("wifi_id", wifi);
    flag = 1;
  }
  if (strcmp(temp_pwd, "") != 0 && strcmp(temp_pwd, pwd) != 0)
  {
    memcpy(pwd, temp_pwd, CC_CONFIG_WIFI_MAX_PWD_LEN);
    cc_fs_set_string("wifi_pwd", pwd);
    flag = 1;
  }
  cc_log("wifi:%s pwd:%s", wifi, pwd);
  if (flag)
    cc_reboot();
}
CC_COMMAND_SHELL(show_wifi, "show or set wifi name and pwd");

void update_wifi_pwd(char* param, int node)
{
  cc_log("recv from node(%d):\r\n", node);

  // cc_printf("\r\nname:%s\r\n", wifi);
  // cc_printf("pwd:%s\r\n", pwd);

  // if (COMMAND_PARA_IS_GIVE(param1))
  // {
  //   memcpy(pwd, param2, 20);
  //   cc_fs_set_string("wifi_pwd", pwd);
  //   cc_printf("new pwd:%s\r\n", pwd);
  //   cc_reboot();
  // }
}
CC_COMMAND_SHELL(update_wifi_pwd, "update wifi pwd");

void update_wifi_name(char* param, int node)
{
  cc_log("recv from node(%d):\r\n", node);
  // if (COMMAND_PARA_IS_GIVE(param1))
  // {
  //   memcpy(wifi, param1, 20);
  //   cc_fs_set_string("wifi_id", wifi);
  //   cc_printf("new wifi:%s\r\n", wifi);
  //   cc_reboot();
  // }
}
CC_COMMAND_SHELL(update_wifi_name, "update wifi name");

#endif